#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    serialport = new QSerialPort(this);

    ui->spinBox->setValue(1);

    // 语法糖，第一个参数是迭代器，遍历第二个列表参数，每遍历一次，执行大括号内容
    // 参数是一个串口类的引用，自动搜索串口，并用留的方式保存到字符串列表里
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        serialNamePort << info.portName();
    }

    ui->comboBox_serial->addItems(serialNamePort);

    connect(ui->pushButton_open_serial, SIGNAL(clicked()), this, SLOT(slot_open_serial()));
    connect(ui->pushButton_close_serial, SIGNAL(clicked()), this, SLOT(slot_close_serial()));
    connect(serialport, SIGNAL(readyRead()), this, SLOT(slot_read_ready()));

    connect(ui->pushButton_charge, SIGNAL(clicked()), this, SLOT(slot_charge()));
    connect(ui->pushButton_nocharge, SIGNAL(clicked()), this, SLOT(slot_nocharge()));

    connect(ui->pushButton_screw_stop, SIGNAL(clicked()), this, SLOT(slot_screw_stop()));
    connect(ui->pushButton_screw_out, SIGNAL(clicked()), this, SLOT(slot_screw_out()));
    connect(ui->pushButton_screw_in, SIGNAL(clicked()), this, SLOT(slot_screw_in()));

    connect(ui->pushButton_relay_close, SIGNAL(clicked()), this, SLOT(slot_relay_close()));
    connect(ui->pushButton_relay_zidong, SIGNAL(clicked()), this, SLOT(slot_relay_zidong()));
    connect(ui->pushButton_relay_shoudong, SIGNAL(clicked()), this, SLOT(slot_relay_shoudong()));

    connect(ui->pushButton_get_state, SIGNAL(clicked()), this, SLOT(slot_get_state()));

    connect(ui->pushButton_door_get, SIGNAL(clicked()), this, SLOT(slot_door_get()));
    connect(ui->pushButton_door_open, SIGNAL(clicked()), this, SLOT(slot_door_open()));
    connect(ui->pushButton_door_close, SIGNAL(clicked()), this, SLOT(slot_door_close()));
    connect(ui->pushButton_door_stop, SIGNAL(clicked()), this, SLOT(slot_door_stop()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::display_text(QByteArray SendData, int direction)
{
    QString current_date = QDateTime::currentDateTime().toString("[hh:mm:ss] ");

    if (direction == SEND) {
        ui->plainTextEdit->appendPlainText(current_date + "send:");
    } else if (direction == RECV) {
        ui->plainTextEdit->appendPlainText(current_date + "recv:");
    }

    cursor= ui->plainTextEdit->textCursor();
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    ui->plainTextEdit->setTextCursor(cursor);


    for (int i=0; i<SendData.length(); i++)
    {
        ui->plainTextEdit->insertPlainText(SendData.mid(i,1).toHex());
        ui->plainTextEdit->insertPlainText(" ");
    }
    qDebug() << SendData.toHex();
}

void MainWindow::slot_open_serial()
{
    serialport->setPortName(ui->comboBox_serial->currentText());
    serialport->setBaudRate(QSerialPort::Baud9600);
    serialport->setDataBits(QSerialPort::Data8);
    serialport->setStopBits(QSerialPort::OneStop);
    serialport->setParity(QSerialPort::NoParity);

    //如果串口已经打开了 先给他关闭了
    if(serialport->isOpen())
    {
        serialport->clear();
        serialport->close();
    }

    if(serialport->open(QIODevice::ReadWrite) == true) {
        QMessageBox::information(this, "提示", "成功打开串口");
    } else {
        QMessageBox::critical(this, "警告", "打开串口失败");
    }
}

void MainWindow::slot_close_serial()
{
    serialport->close();
}

void MainWindow::slot_read_ready()
{
    display_text(serialport->readAll(), RECV);
}

void MainWindow::slot_charge()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x00, 0x00, 0x01, 0x48, 0x0A};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_nocharge()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x00, 0x00, 0x02, 0x08, 0x0B};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_screw_stop()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0xD8, 0x0A};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_screw_out()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x01, 0x00, 0x01, 0x19, 0xCA};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_screw_in()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x01, 0x00, 0x02, 0x59, 0xCB};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_relay_close()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x02, 0x00, 0x00, 0x28, 0x0A};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_relay_zidong()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x02, 0x00, 0x01, 0xE9, 0xCA};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_relay_shoudong()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x06, 0x00, 0x02, 0x00, 0x02, 0xA9, 0xCB};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_get_state()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x01, id, 0x01, 0x03, 0x00, 0x10, 0x00, 0x0A, 0xC4, 0x08};
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_door_get()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x02, id, id, 0x00, 0x00, 0x00};
    buf[5] = (buf[2] ^ buf[3] ^ buf[4]);
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_door_open()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x02, id, id, 0x03, 0x00, 0x00};
    buf[5] = (buf[2] ^ buf[3] ^ buf[4]);
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_door_close()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x02, id, id, 0x05, 0x00, 0x00};
    buf[5] = (buf[2] ^ buf[3] ^ buf[4]);
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}

void MainWindow::slot_door_stop()
{
    uint8_t id = ui->spinBox->value();
    uint8_t buf[] = {0x02, id, id, 0x01, 0x00, 0x00};
    buf[5] = (buf[2] ^ buf[3] ^ buf[4]);
    QByteArray array((char*)buf, sizeof(buf));
    serialport->write(array);
    display_text(array, SEND);
}




